package com.zqxx.examine.management.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zqxx.examine.management.persistence.QuestionHistoryMapper;
import com.zqxx.examine.model.exam.UserQuestionHistory;
import com.zqxx.examine.model.question.QuestionStatistic;

@Service("questionHistoryService")
public class QuestionHistoryService 
{
  @Autowired
  private QuestionHistoryMapper questionHistoryMapper;
  
  public void addUserQuestionHist(List<UserQuestionHistory> historyList)
  {
    this.questionHistoryMapper.addUserQuestionHist(historyList);
  }
  
  public void addUserQuestionHist(UserQuestionHistory... history)
  {
    List<UserQuestionHistory> historyList = new ArrayList();
    for (UserQuestionHistory h : history) {
      historyList.add(h);
    }
    this.questionHistoryMapper.addUserQuestionHist(historyList);
  }
  
  public Map<Integer, List<UserQuestionHistory>> getUserQuestionHist(int userId, int fieldId)
  {
    List<UserQuestionHistory> histList = this.questionHistoryMapper.getUserQuestionHist(userId, fieldId);
    Map<Integer, List<UserQuestionHistory>> map = new HashMap();
    for (UserQuestionHistory hist : histList)
    {
      List<UserQuestionHistory> result = (List)map.get(Integer.valueOf(hist.getPointId()));
      if (result == null) {
        result = new ArrayList();
      }
      result.add(hist);
      map.put(Integer.valueOf(hist.getPointId()), result);
    }
    return map;
  }
  
  public Map<Integer, QuestionStatistic> getQuestionHistStaticByFieldId(int fieldId, int userId)
  {
    List<QuestionStatistic> statisticList = this.questionHistoryMapper.getQuestionHistStaticByFieldId(fieldId, userId);
    Map<Integer, QuestionStatistic> map = new HashMap();
    for (QuestionStatistic statistic : statisticList) {
      map.put(Integer.valueOf(statistic.getPointId()), statistic);
    }
    return map;
  }
  
  public Map<Integer, Map<Integer, QuestionStatistic>> getTypeQuestionHistStaticByFieldId(int fieldId, int userId)
  {
    List<QuestionStatistic> statisticList = this.questionHistoryMapper.getTypeQuestionHistStaticByFieldId(fieldId, userId);
    Map<Integer, Map<Integer, QuestionStatistic>> map = new HashMap();
    for (QuestionStatistic statistic : statisticList)
    {
      Map<Integer, QuestionStatistic> tmp = (Map)map.get(Integer.valueOf(statistic.getPointId()));
      if (tmp == null) {
        tmp = new HashMap();
      }
      tmp.put(Integer.valueOf(statistic.getQuestionTypeId()), statistic);
      map.put(Integer.valueOf(statistic.getPointId()), tmp);
    }
    return map;
  }
}
